<template>
	<view>
		<view class="address-wrapper" @click="getAddress">
			<view class="address" v-if="myAddr.userName">
				<view class="receiver">
					<view class="name">收货人：{{myAddr.userName}}</view>
					<view class="phone-num">{{myAddr.telNumber}}</view>
					<text class="iconfont icon-arrow-right"></text>
				</view>
				<view class="address-txt">收货地址：{{myAddr.provinceName +' '+ myAddr.cityName + ' ' +myAddr.detailInfo}}</view>
			</view>
			<!-- 选择地址 -->
			<view class="choose-address" v-else>
				<view>请选择地址</view>
				<text class="iconfont icon-arrow-right"></text>
			</view>
			<view class="flower">
				<image src="/static/images/cart_border@2x.png"></image>
			</view>
		</view>

		<!-- 商品列表 -->
		<!-- 商品列表 -->
		<view class="goods-list">
			<view class="goods-item" v-for="(item, index) in goodsCart" :key="index" @click="toItem(item.goodsId)">
				<!-- <text class="iconfont" :class="item.checked?'icon-checked':'icon-unchecked'" @click.stop="toggleCheck(item)"></text> -->
				<image :src="item.goods_small_logo" alt="" />
				<view class="right">
					<text class="text-line2">{{item.goods_name}}</text>
					<view class="btm">
						<text class="price">￥<text>{{item.goods_price}}</text>.00</text>
						<view class="goods-num">
							<button @click.stop="decrease(item, index)">-</button>
							<text>{{item.num}}</text>
							<button @click.stop="add(item)">+</button>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="bottom-fixed" @click="wxPay">
			微信支付({{AllPrice}}.00)
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				myAddr: uni.getStorageSync('addr') || {},
				goodsCart: []
			}
		},
		computed: {
				// 总价
				AllPrice() {
					// 选中的商品数量*商品价格之和
					return this.goodsCart.reduce((sum, item) => {
						return sum + item.num * item.goods_price;
					}, 0);
				}
			},
		onLoad(options) {
			// 如果goodsId有值,表示立即购买，优先级比购物车跳转更高
			this.goodsId = parseInt(options.goodsId);

			if (this.goodsId) {
				// 构造一个和storage cart一样的结构
				this.cart = [{
					goodsId: this.goodsId,
					num: 1,
					checked: true
				}];
			} else {
				// 取storage cart里面每一项goodsId以,分隔
				this.cart = uni.getStorageSync('cart') || [];
				// 返回cart数组勾选的商品
				this.cart = this.cart.filter(item => {
					return item.checked;
				});
			}

			// 如果storage cart不为空的,才发请求
			if (this.cart.length) {
				this.queryGoodsList();
			}
		},
		methods: {
			//微信支付
			toPay(){
				
			},
			
			// 创建订单
			// async getCreate(){
			// 	let itemCreate = await this.$request({
			// 		url:'/api/public/v1/my/orders/create',
			// 		method:'post'
			// 	})
			// }
			
			// 请求商品列表
			async queryGoodsList() {
				// let idStr =''
				// cart.forEach(item=>{
				// 	idStr+=item.goodsId+','
				// })
				// TODO把最后这个逗号去掉

				// goodsId数组,然后join拼接字符串
				let newId = this.cart.map(item => {
					return item.goodsId;
				});

				// 数组元素以逗号分隔
				let idStr = newId.join(',');

				this.goodsList = await this.$request({
					url: '/api/public/v1/goods/goodslist?goods_ids=' + idStr
				});

				// 请求结束后合并数组，以goodsCart为主
				this.goodsCart = this.cart.map(item => {
					let cartGoods = this.goodsList.find(goitem => {
						// 将goodsList与goodsCart两个数组中goodsId相同的合并在一起
						return goitem.goods_id === item.goodsId
					})
					return { ...item,
						...cartGoods
					}
				})
			},

			// 授权获取通讯址
			getAddress() {
				// 发起授权
				uni.authorize({
					scope: 'scope.address',
					success: () => {
						// 允许
						// 调接口
						uni.chooseAddress({
							success: res => {
								console.log(res);
								this.myAddr = res;
								uni.setStorageSync('addr', res);
							}
						});
					},
					fail: () => {
						// 拒绝
						// 提示打开
						uni.showModal({
							title: '提示',
							content: '请在设置中允许通讯地址',
							success: res => {
								if (res.confirm) {
									uni.openSetting();
								}
							}
						});
					}
				})
			}
		}
	}
</script>
<style lang="less">
	.address-wrapper {
		background-color: #fff;
	}

	.address {
		display: flex;
		flex-direction: column;
		padding: 44rpx 30rpx 48rpx 20rpx;

		.receiver {
			display: flex;
			justify-content: space-between;
			margin-bottom: 50rpx;
			position: relative;

			.phone-num {
				margin-right: 40rpx;
			}

			.icon-arrow-right {
				position: absolute;
				top: 8rpx;
				right: 0;
				color: #999;
			}
		}
	}

	.choose-address {
		display: flex;
		padding: 44rpx 30rpx 48rpx 20rpx;
		justify-content: space-between;

		.icon-arrow-right {
			color: #999;
		}
	}

	.flower {
		image {
			height: 16rpx;
			width: 100%;
			display: block;
		}
	}

	.goods-item {
		height: 206rpx;
		background-color: #fff;
		border-top: 1rpx solid #ddd;
		display: flex;
		box-sizing: border-box;
		align-items: center;

		image {
			width: 160rpx;
			height: 160rpx;
			margin-left: 30rpx;
		}

		.right {
			flex: 1;
			display: flex;
			flex-direction: column;
			margin: 0 20rpx 0 18rpx;

			.btm {
				display: flex;
				margin-top: 40rpx;
				justify-content: space-between;

				.price {
					color: #eb4450;

					>text {
						font-size: 24px;
					}
				}

				.goods-num {
					display: flex;
					align-items: center;

					button {
						width: 60rpx;
						height: 50rpx;
						box-sizing: border-box;
						display: flex;
						justify-content: center;
						align-items: center;
						border: 4rpx solid #666;
					}

					text {
						margin: 0 30rpx;
					}
				}
			}
		}
	}

	.goods-list {
		position: absolute;
		bottom: 98rpx;
		top: 243rpx;
		width: 100%;
		overflow: auto;
		padding-bottom: 60rpx;
		background-color: #f4f4f4;
	}

	.bottom-fixed {
		position: absolute;
		height: 98rpx;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: #1aad19;
		color: #fff;
		line-height: 98rpx;
		text-align: center;
	}
</style>
